#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author  : ganuoxun
# @description : 主要的数据model，对应各自的表结构

from typing import Optional, List, Any
from datetime import datetime
from pydantic import BaseModel
import json


class UpdateOdsModel(BaseModel):
    """
    贴源层数据结构
    """
    lngid: str
    rawid: Optional[str] = None
    process_id: Optional[str] = None
    source_type: Optional[int] = None
    type_id: Optional[int] = None
    file_name: List[str]
    latest_date: Optional[str] = None
    upload_time: Optional[datetime] = None
    status: int
    error_info: Optional[str] = None

    def get_dict(self) -> dict:
        """
        将对象转换为字段格式
        Returns:
            dict
        """
        return {
            "_id": self.lngid,
            "lngid": self.lngid,
            "rawid": self.rawid,
            "process_id": self.process_id,
            "source_type": self.source_type,
            "type_id": self.type_id,
            "file_name": self.file_name,
            "latest_date": self.latest_date,
            "upload_time": self.upload_time,
            "status": self.status,
            "error_info": self.error_info
        }


class OdsResultModel(BaseModel):
    """
    贴源层解析结果数据结构
    """
    lngid: str
    rawid: str
    process_id: str
    source_type: int
    type_id: int
    latest_date: str
    result: dict

    def get_dict(self) -> dict:
        """
        将对象转换为字段格式
        Returns:
            dict
        """
        return {
            "lngid": self.lngid,
            "rawid": self.rawid,
            "process_id": self.process_id,
            "source_type": self.source_type,
            "type_id": self.type_id,
            "latest_date": self.latest_date,
            "result": self.result
        }

    def get_metadata(self):
        """
        将对象转换为gridfs的metadata
        Returns:
            dict
        """
        return {
            "lngid": self.lngid,
            "rawid": self.rawid,
            "process_id": self.process_id,
            "source_type": self.source_type,
            "type_id": self.type_id,
            "latest_date": self.latest_date
        }

    def get_result(self):
        """
        返回result的字符串
        Returns:

        """
        return json.dumps(self.result, ensure_ascii=False)


class UpdateDwdModel(BaseModel):
    """
    明细层数据结构
    """
    m_id: str # 这里本应该使用 _id 但 pydantic 不允许使用 _id 在 get_dict 中转换
    ods_id: str
    user_id: Any
    keyid: str
    rawid: str
    process_id: str
    source_type: int
    type_id: int
    is_deprecated: int
    latest_date: str
    update_date: str
    title_info: Optional[List[dict]] = None
    person_info: Optional[dict] = None
    place_info: Optional[dict] = None
    date_info: Optional[List[dict]] = None

    def get_dict(self) -> dict:
        """
        将对象转换为字段格式
        Returns:
            dict
        """
        return {
            "_id": self.m_id,
            "ods_id": self.ods_id,
            "user_id": self.user_id,
            "keyid": self.keyid,
            "rawid": self.rawid,
            "process_id": self.process_id,
            "source_type": self.source_type,
            "type_id": self.type_id,
            "is_deprecated": self.is_deprecated,
            "latest_date": self.latest_date,
            "update_date": self.update_date,
            "title_info": self.title_info,
            "person_info": self.person_info,
            "place_info": self.place_info,
            "date_info": self.date_info
        }
